package com.wanmait.cookbook.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.wanmait.cookbook.dao.CookbookCollectDAO;
import com.wanmait.cookbook.dao.CookbookDAO;

import com.wanmait.cookbook.vo.Cookbook;
import com.wanmait.cookbook.vo.Pager;
import com.wanmait.cookbook.vo.Userinfo;

/**
 * Servlet implementation class HotCookbookShowListServlet
 */
@WebServlet("/HotCookbookShowListServlet")
public class HotCookbookShowListServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public HotCookbookShowListServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String action = request.getParameter("action");
		if ("getHotCookbookShowList".equals(action)) {
			this.getHotCookbookShowList(request, response);
		} else if ("getPagerCookbook".equals(action)) {
			this.getPagerCookbook(request, response);
		} else if ("getOnePageCookbook".equals(action)) {
			this.getOnePageCookbook(request, response);
		}
	}

	// �õ���һҳ����
	public void getOnePageCookbook(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		HttpSession session=request.getSession();
		Pager pager = new Pager();
		pager.setPageSize(3);
		int rows = CookbookDAO.getCookbookDAO().getCookbookRows();
		pager.setPageCount(rows);
		pager.setPageNo(1);
		List<Cookbook> getPagerCookbook = CookbookDAO.getCookbookDAO().getPagerCookbook(pager);
		List<Integer> cookbookCount = new ArrayList<Integer>();
		List<Boolean> isCollects=new ArrayList<Boolean>();
		Userinfo userinfo=(Userinfo) session.getAttribute("userinfo");
		for (Cookbook cookbook : getPagerCookbook) {
			int count = CookbookCollectDAO.getCollectCookbookCollectDAO().coolectCount(cookbook);
			cookbookCount.add(count);
			if(userinfo!=null) {
				
				boolean isCollect=CookbookCollectDAO.getCollectCookbookCollectDAO().isCollect(userinfo.getId(), cookbook.getId());
				isCollects.add(isCollect);
			}
		}
		request.setAttribute("isCollects", isCollects);
		request.setAttribute("pager", pager);
		request.setAttribute("cookbookCount", cookbookCount);
		request.setAttribute("getPagerCookbook", getPagerCookbook);
		request.getRequestDispatcher("/hotCookbookShowList.jsp").forward(request, response);

	}

	// ����ajax�õ����в���
	public void getPagerCookbook(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		String pageNo = request.getParameter("pageNo");
		
		HttpSession session=request.getSession();
		Pager pager = new Pager();
		pager.setPageSize(3);
		int rows = CookbookDAO.getCookbookDAO().getCookbookRows();
		pager.setPageCount(rows);
		if(Integer.parseInt(pageNo)<1) {
			pageNo=1+"";
		}
		if(Integer.parseInt(pageNo)>pager.getPageCount()) {
			pageNo=pager.getPageCount()+"";
		}
		
		pager.setPageNo(Integer.parseInt(pageNo));
		List<Cookbook> getPagerCookbook = CookbookDAO.getCookbookDAO().getPagerCookbook(pager);
		List<Integer> cookbookCount = new ArrayList<Integer>();
		List<Boolean> isCollects=new ArrayList<Boolean>();
		Userinfo userinfo=(Userinfo) session.getAttribute("userinfo");
		for (Cookbook cookbook : getPagerCookbook) {
			int count = CookbookCollectDAO.getCollectCookbookCollectDAO().coolectCount(cookbook);
			cookbookCount.add(count);
			if(userinfo!=null) {
				
				boolean isCollect=CookbookCollectDAO.getCollectCookbookCollectDAO().isCollect(userinfo.getId(), cookbook.getId());
				isCollects.add(isCollect);
			}
		}
		request.setAttribute("isCollects", isCollects);
		request.setAttribute("pager", pager);
		request.setAttribute("cookbookCount", cookbookCount);
		request.setAttribute("getPagerCookbook", getPagerCookbook);
		request.getRequestDispatcher("/inc/ajaxInc/hotCookbookInc.jsp").forward(request, response);
	}

	// �õ���ҳ��ʵ�Ĳ���
	protected void getHotCookbookShowList(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		List<Cookbook> allHotCookbook = CookbookDAO.getCookbookDAO().getAllHotCookbook();

		List<Integer> cookbookCount = new ArrayList<Integer>();

		for (Cookbook cookbook : allHotCookbook) {
			int count = CookbookCollectDAO.getCollectCookbookCollectDAO().coolectCount(cookbook);
			System.out.println(count);
			cookbookCount.add(count);
		}
		request.setAttribute("cookbookCount", cookbookCount);
		request.setAttribute("allHotCookbook", allHotCookbook);
		request.getRequestDispatcher("/hotCookbookShowList.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
